package org.example.flatmap;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class Java8FlatMap5 {

    public static void main(String[] args) {
        Developer o1 = new Developer();
        o1.setName("mkyong");
        o1.addBook("Java 8 in Action");
        o1.addBook("Spring Boot in Action");
        o1.addBook("Effective Java (3nd Edition)");

        Developer o2 = new Developer();
        o2.setName("zilap");
        o2.addBook("Learning Python, 5th Edition");
        o2.addBook("Effective Java (3nd Edition)");

        List<Developer> list = new ArrayList<>();
        list.add(o1);
        list.add(o2);

        List<String> result = list.stream().map(x -> x.getBooks())
                .flatMap(x -> x.stream())
                .filter(x -> !x.toLowerCase().contains("Python"))
                .collect(Collectors.toList());
        System.err.println(JSONObject.toJSONString(result,true));

    }

    static class Developer{
        private Integer id;
        private String name;
        private Set<String> books;

        public Developer() {
        }

        public Developer(Integer id, String name, Set<String> books) {
            this.id = id;
            this.name = name;
            this.books = books;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Set<String> getBooks() {
            return books;
        }

        public void setBooks(Set<String> books) {
            this.books = books;
        }

        public void addBook(String book){
            if (this.books == null) {
               this.books = new HashSet<>();
            }
            this.books.add(book);
        }
    }
}
